package com.thb.pay.agent.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Lists;
import com.thb.pay.agent.entity.AgentInfo;
import com.thb.pay.agent.service.AgentInfoService;
import com.thb.pay.api.agent.AgentInfoServiceApi;
import com.thb.pay.api.agent.vo.req.AgentInfoPageReq;
import com.thb.pay.api.agent.vo.req.AgentInfoReq;
import com.thb.pay.api.agent.vo.resp.AgentInfoVo;
import com.thb.pay.core.utils.PageResult;
import com.xiaoleilu.hutool.bean.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 代理商-信息表 前端控制器
 * </p>
 *
 * @author lucky
 * @since 2019-07-21
 */
@RestController
public class AgentInfoController implements AgentInfoServiceApi {

    @Autowired
    private AgentInfoService agentInfoService;

    @Override
    public PageResult<AgentInfoVo> findPage(@RequestBody AgentInfoPageReq pageReq) {
        return agentInfoService.findPage(pageReq);
    }

    @Override
    public List<AgentInfoVo> findListByAgent(@RequestBody AgentInfoReq agentInfoReq) {
        List<AgentInfoVo> resultDataList = Lists.newArrayList();
        LambdaQueryWrapper<AgentInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AgentInfo::getLevel, agentInfoReq.getLevel());
        List<AgentInfo> agentInfoList = agentInfoService.selectList(queryWrapper);
        agentInfoList.forEach(agentInfo -> {
            AgentInfoVo resultData = new AgentInfoVo();
            BeanUtil.copyProperties(agentInfo, resultData);
            resultDataList.add(resultData);
        });
        return resultDataList;
    }

    @Override
    public void add(@RequestBody AgentInfoReq agentInfo) {
        agentInfoService.add(agentInfo);
    }

    @Override
    public AgentInfoVo getByLoginName(@RequestParam("loginName") String loginName) {
        return agentInfoService.getByLoginName(loginName);
    }

    @Override
    public AgentInfoVo getByAgentId(@RequestParam("agentId") Long agentId) {
        return agentInfoService.getByAgentId(agentId);
    }

    @Override
    public void update(AgentInfoReq agentInfoReq) {
        agentInfoService.update(agentInfoReq);
    }
}
